<?php
class SysclassAction extends Action{
    public function _initialize(){//初始化验证
	     header("Content-type:text/html;charset=utf-8");
	     check_pression();
	}
    public function index(){//管理
	   $col_id=$_GET["col_id"];
	   $this->assign("col_id",$col_id);
	   
	   $parid=saferequest($_GET["parid"]);
	   if(empty($parid)) $parid=0;
	   $this->assign("parid",$parid);
	   $sysclass=M("sysclass");
	   $maxseq=$sysclass->where("parid=$parid and col_id=$col_id")->max("sequence");
	   $minseq=$sysclass->where("parid=$parid and col_id=$col_id")->min("sequence");
	   $sysclasss=$sysclass->where("parid=$parid and col_id=$col_id")->order("sequence desc")->findAll();
	   $this->assign("minseq",$minseq);
	   $this->assign("maxseq",$maxseq);
	   $this->assign("sysclasss",$sysclasss);
	   $this->display();
	}
	
	public function add(){
	   $col_id=$_GET["col_id"];
	   $this->assign("col_id",$col_id);
	   
	   $parid=saferequest($_GET["parid"]);
	   if(empty($parid)) $parid=0;
	   $this->assign("parid",$parid);
	   $this->display();
	}
    
	public function insert(){//添加
	    $parid=$_GET["parid"];
		$col_id=$_GET["col_id"];
		$sysclass=D("sysclass");
		$maxseq=$sysclass->where("parid = $parid and col_id=$col_id")->max("sequence");
		$maxseq++;
		$_POST["sequence"]=$maxseq;
		$_POST["parid"]=$parid;
		$_POST["col_id"]=$col_id;
		
		if(!empty($parid)){
		     $parpath=$sysclass->where("id=$parid")->getField("parpath");
		     $p=$parpath.",".$parid;
		     $_POST["parpath"]=$p;
		}else{
		     $_POST["parpath"]=0;
		}
		
		if($vo=$sysclass->create()){
		    $sysclass_id=$sysclass->add();
			if($sysclass_id!==false){
			   //$this->success("操作成功");
			   $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id),1,"操作成功");
			}else{
			   $this->error("操作失败");
			}
		}else{
		   $this->error($sysclass->getError());
		}
	}
	
	public function edit(){//修改
	   $id=saferequest($_GET["id"]);
	   $parid=$_GET["parid"];
	   $this->assign("parid",$parid);
	   $col_id=$_GET["col_id"];
	   $this->assign("col_id",$col_id);
	   $sysclass=M("sysclass");
	   $sysclasss=$sysclass->where("id = $id")->find();
	   $this->assign("sysclasss",$sysclasss);
	   $this->display();
	}
     
	public function update(){
	   $parid=$_GET["parid"];
	   $col_id=$_GET["col_id"];
	   $sysclass=D("sysclass");
	   if($sysclass->create()){
	      $result=$sysclass->save();
	      if($result!==false){
		     //$this->success("操作成功");
			 $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id),1,"操作成功");
		  }else{
		     $this->error("操作失败");
		  }
	   }else{
	      $this->error($sysclass->getError());
	   }
	
	}
	 
	public function del(){//删除
	  $col_id=$_GET["col_id"];
	  $parid=$_GET["parid"];
	  $id=saferequest($_GET["id"]);
	  if(empty($id)) $this->error("删除项不存在");
	  $sysclass=M("sysclass");
	  
	  $sysclass->startTrans();//启动事务
	  $result=$sysclass->delete($id);
	  $childres=$sysclass->where("parpath like '%,$id' or parpath like '%,$id,%'")->delete();//删除子项目
	  if($result!==false&&$childres!==false){
	     $sysclass->commit();//事务提交
	     $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id),1,"操作成功");
	  }else{
	     $sysclass->rollback();//事务回滚
	     $this->error("操作失败");
	  }
	}
	
	public function up(){//上调
	   $id=saferequest($_GET["id"]);
	   $col_id=saferequest($_GET["col_id"]);
	   $sysclass=M("sysclass");
	   $c=$sysclass->where("id=$id")->find();
	   
	   $seq=$c["sequence"];
	   $parid=$c["parid"];
	       
	   $n=$sysclass->where("parid=$parid and sequence > $seq and col_id=$col_id")->order("sequence asc")->find();
	   
	   $nextseq=$n["sequence"];
	   $nextid=$n["id"];

       //修改
	   $sysclass->where("id=$nextid")->setField("sequence",$seq);
	   $sysclass->where("id=$id")->setField("sequence",$nextseq);
	   $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id));
	}
	
	public function down(){//下调
	   $id=saferequest($_GET["id"]);
	   $col_id=saferequest($_GET["col_id"]);
	   $sysclass=M("sysclass");
	   $c=$sysclass->where("id=$id")->find();
	
	   $seq=$c["sequence"];
	   $parid=$c["parid"];
	   
	   $n=$sysclass->where("parid=$parid and sequence < $seq and col_id=$col_id")->order("sequence desc")->find();
	   
	   $nextseq=$n["sequence"];
	   $nextid=$n["id"];
       //修改
	   $sysclass->where("id=$nextid")->setField("sequence",$seq);
	   $sysclass->where("id=$id")->setField("sequence",$nextseq);
       $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id));
	}
	public function set_hide(){
	   checkget();
	   $id=$_GET["id"];
	   $col_id=$_GET["col_id"];
	   $sysclass=M("sysclass");
	   
	   $sysclass->where("id=$id")->setField("is_show","否");
	   $parid=$sysclass->where("id=$id")->getField("parid");
	   $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id));
	}
	public function set_show(){
	   checkget();
	   $id=$_GET["id"];
	   $col_id=$_GET["col_id"];
	   $sysclass=M("sysclass");
	   
	   $sysclass->where("id=$id")->setField("is_show","是");
	   $parid=$sysclass->where("id=$id")->getField("parid");
	   $this->redirect("index",array("parid"=>$parid,"col_id"=>$col_id));
	}
}
?>